<?php

class Friend extends CI_Model {

    var $name = '';
    var $image = '';
    var $sex = '';
    var $birthdate = '';
    var $fbuser_id = '';
    var $user_id = '';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function diffArray($array1, $array2) {
        $result = array();
        foreach ($array1 as $item) {
            $item = trim($item);
            if (!in_array($item, $array2)) {
                $result[] = $item;
            }
        }
        return $result;
    }

    function getFriendInfo($id) {
        $query = "SELECT * FROM friend,friend_favorite,friend_point 
                  WHERE id=$id AND friend.id=friend_favorite.friend_id
                        And friend_point.friend_id = friend.id
                        AND friend_point.datetime = (SELECT friend_point.datetime FROM friend_point WHERE friend_id=$id ORDER BY friend_point.datetime DESC LIMIT 0,1)
                 ";
        //echo $query;
        $data = $this->db->query($query)->result();
        $data[0]->friend_matchbooks = array_filter(explode(",", $data[0]->friend_matchbooks));
        $data[0]->friend_matchmovies = array_filter(explode(",", $data[0]->friend_matchmovies));
        $data[0]->friend_matchmusic = array_filter(explode(",", $data[0]->friend_matchmusic));

        $data[0]->friend_books = array_filter(explode(",", $data[0]->friend_books));
        $data[0]->friend_movies = array_filter(explode(",", $data[0]->friend_movies));
        $data[0]->friend_music = array_filter(explode(",", $data[0]->friend_music));

        $data[0]->friend_books = $this->diffArray($data[0]->friend_books, $data[0]->friend_matchbooks);
        $data[0]->friend_movies = $this->diffArray($data[0]->friend_movies, $data[0]->friend_matchmovies);
        $data[0]->friend_music = $this->diffArray($data[0]->friend_music, $data[0]->friend_matchmusic);
        $data[0]->image = "https://graph.facebook.com/" . $data[0]->image . "/picture";
        return $data[0];
    }

    function issetFriend($fbuser_id, $user_id) {

        $result = $this->db->get_where('friend', array('fbuser_id' => $fbuser_id, 'user_id' => $user_id));
        if (count($result->result()) > 0) {

            return TRUE;
        }
        return FALSE;
    }
    function cmpRanListObj($a, $b)
    {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0;
        }
        return ($al > $bl) ? +1 : -1;
    }

    function getRankList($fbuser_id, $limit = 10, $type = 0, $sex = "", $friendsString = "", $orderby = "ASC") {
        $where = "";
        if ($sex != "") {
            $where.= " AND friend.sex = '" . $sex . "'";
        }
        if (!is_array($friendsString) && $friendsString != "") {
            $where.= " AND friend.fbuser_id $friendsString";
        }
        //get time
        $query = "SELECT friend.id,friend.name,friend.image,friend_point.point,friend_point.position,friend_point.datetime,friend.fbuser_id FROM user,friend,friend_point
                  WHERE user.fbuser_id = $fbuser_id
                    AND friend_point.user_id=user.id
                    AND friend_point.friend_id = friend.id
                    AND friend_point.cate_type_id = $type
                    "
                . $where .
                "
                    AND friend_point.datetime = (SELECT friend_point.datetime FROM friend_point,user WHERE friend_point.cate_type_id = $type AND user.fbuser_id = $fbuser_id AND user.id = friend_point.user_id ORDER BY friend_point.datetime DESC LIMIT 0,1)
                    ORDER BY friend_point.position $orderby LIMIT 0,$limit
                ";
        //echo $query;
        //echo $query;
        $FriendsCurrentPosition = $this->db->query($query)->result();
        if (count($FriendsCurrentPosition) == 0) {
            return array();
        }
        //get old position
        $datetime = "";
        $FriendsIdString = array();
        foreach ($FriendsCurrentPosition as $item) {
            $FriendsIdString[] = $item->id;

            $datetime = $item->datetime;
        }
        $FriendsIdString = implode(",", $FriendsIdString);
        //echo $FriendsIdString;
        $query = "SELECT friend_id,position,point,datetime FROM friend_point
                  WHERE friend_id IN ($FriendsIdString) AND datetime = (SELECT friend_point.datetime FROM friend_point,user WHERE friend_point.cate_type_id = $type AND user.fbuser_id = $fbuser_id AND user.id = friend_point.user_id AND friend_point.datetime<'$datetime' ORDER BY friend_point.datetime DESC LIMIT 0,1)
                ";
        $FriendsOldPosition = $this->db->query($query)->result();
        $data = array();
        $count = 0;
        $pointsArray = array();
        $flagPoint = 0;
        ;
        foreach ($FriendsCurrentPosition as $item) {


            $item->oldposition = "";
            $item->image = "https://graph.facebook.com/" . $item->fbuser_id . "/picture";
            if (is_array($friendsString) && in_array($item->fbuser_id, $friendsString)) {
                $item->isfriend = 1;
            } else {
                if (is_string($friendsString) && $friendsString != "") {
                    if ($_REQUEST['isfriend'] == 1) {
                        $item->isfriend = 1;
                    } else {
                        $item->isfriend = 0;
                    }
                } else {
                    $item->isfriend = 0;
                }
            }
            foreach ($FriendsOldPosition as $old) {
                if ($item->id == $old->friend_id) {
                    if ($item->position < $old->position) {
                        $item->oldposition = 1;
                    }
                    if ($item->position > $old->position) {
                        $item->oldposition = -1;
                    }
                    if ($item->position == $old->position) {
                        $item->oldposition = 0;
                    }
                }
            }
            $data[$item->point][] = $item;
        }
        $buffData = array();
        
        foreach($data as $item){
            usort($item, array("Friend", "cmpRanListObj"));
            foreach($item as $obj){
                $buffData[] = $obj;
            }
        }
        $data = $buffData;
        //print_r($data);die; 
        //print_r($pointsArray);die;
        //get max of list
        $query = "SELECT count(*) as max FROM user,friend,friend_point
                  WHERE user.fbuser_id = $fbuser_id
                    AND friend_point.user_id=user.id
                    AND friend_point.friend_id = friend.id
                    AND friend_point.cate_type_id = $type
                    "
                . $where .
                "
                    AND friend_point.datetime = '$datetime'
                ";
        //echo $query;
        $number_maxlist = $this->db->query($query)->result();
        $result = array();
        $result['data'] = $data;
        $result['total'] = $number_maxlist[0]->max;
        return $result;
    }

    function getIdOfUser($fbuser_id) {
        $this->db->select('id');
        $this->db->from('friend');
        $this->db->where('fbuser_id', $fbuser_id);
        $result = $this->db->get();
        return $result->result();
    }

    function updateFriend($FriendInfo, $user_id, $point, $position, $type = 0) {
        $this->fbuser_id = $FriendInfo['uid']; // please read the below note
        $this->sex = $FriendInfo['sex'];
        $this->birthdate = $FriendInfo['birthday_date'];
        $this->image = $FriendInfo['uid'];
        $this->name = $FriendInfo['name'];
        $this->user_id = $user_id;

        $friend_favorite = $this->friend_favorite;

        $fb_books = $FriendInfo['books'];
        $fb_movies = $FriendInfo['movies'];
        $fb_music = $FriendInfo['music'];
        //insert friend info
        $this->db->where('fbuser_id', $FriendInfo['uid']);
        $this->db->where('user_id', $user_id);
        $this->db->update('friend', $this);
        $friend_id = $this->getIdOfUser($this->fbuser_id);
        //insert friend point
        $data = array(
            'friend_id' => $friend_id[0]->id,
            'point' => $point,
            'datetime' => date('d/m/Y H:i:s'),
            'user_id' => $user_id,
            'position' => $position,
            'cate_type_id' => $type
        );
        $this->db->insert('friend_point', $data);
        //insert friend favorite

        $friend_favorite->update_friend($friend_id[0]->id, $FriendInfo['friend_books'], $FriendInfo['friend_movies'], $FriendInfo['friend_music'], $fb_books, $fb_movies, $fb_music);
        //insert friend position
    }

    function insertFriend($FriendInfo, $user_id, $point, $position, $type = 0) {
        $this->fbuser_id = $FriendInfo['uid']; // please read the below note
        $this->sex = $FriendInfo['sex'];
        $this->birthdate = $FriendInfo['birthday_date'];
        $this->image = $FriendInfo['uid'];
        $this->name = $FriendInfo['name'];
        $this->user_id = $user_id;

        $friend_favorite = $this->friend_favorite;

        $fb_books = $FriendInfo['books'];
        $fb_movies = $FriendInfo['movies'];
        $fb_music = $FriendInfo['music'];
        //insert friend info
        $this->db->insert('friend', $this);
        $friend_id = $this->db->insert_id();
        //insert friend point
        $data = array(
            'friend_id' => $friend_id,
            'point' => $point,
            'datetime' => date('d/m/Y H:i:s'),
            'user_id' => $user_id,
            'position' => $position,
            'cate_type_id' => $type
        );
        $this->db->insert('friend_point', $data);
        //insert friend favorite
        $friend_favorite->insert_friend($friend_id, $FriendInfo['friend_books'], $FriendInfo['friend_movies'], $FriendInfo['friend_music'], $fb_books, $fb_movies, $fb_music);
        //insert friend position
    }

}